<template>
	<view class="common-car bg-f7">
		<view class="shop-car" v-if="goods && goods.length">
			<view class="shop-car-header padding-l-r-30 bg-white margin-b-20 justifyContent-spaceBetween">
				<text>全部 {{totalNum}}</text>
				<text @tap="cut" class="cut">{{isCut?'管理':'管理完成'}}</text>
			</view> 
			<view class="store-box" >
			<!-- 	<view class="store-header">
					<image src="/static/image/shopCar/select.png" v-if="itemq.checked == 2" class="checked-image" mode="" @tap="storeCheck(indexq,itemq.checked)"></image>
					<image src="/static/image/shopCar/not_select.png" v-else class="checked-image" mode="" @tap="storeCheck(indexq,itemq.checked)">
					</image>
					<text>{{itemq.name}}</text>
					<image src="/static/image/shopCar/youjiantou1.png" class="label" mode=""></image>
				</view> -->
				<view class="goodsInfo" v-for="(itemw,indexw) in goods" :key="indexw">
					<image src="/static/image/shopCar/select.png" v-if="itemw.checked == 2" class="checked-image"
						mode="" @tap="goodsCheck( indexw,itemw.checked)"></image>
					<image src="/static/image/shopCar/not_select.png" v-else class="checked-image" mode=""
						@tap="goodsCheck( indexw,itemw.checked)"></image>
					<view class="goodsInfo-right">
						<image :src="itemw.image" class="goods-image" mode=""></image>
						<view class="goodsInfo-box">
							<text class="goods-name">{{itemw.title}}</text>
							<!-- <text class="spe">规格：{{itemw.remark}}</text> -->
							<view class="goods-box">
								<view class="goods-price">
									¥<text>{{itemw.price}}</text>
								</view>
								<view class="goods-num-box">
									<view class="goods-image" @tap="sub(indexw, itemw.count)">
										<text>-</text>
									</view>
									<view class="goods-num">
										<text>{{itemw.count}}</text>
									</view>
									<view class="goods-image" @tap="add(indexw, itemw.count)">
										<text>+</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="statistics-box padding-l-r-30">
				<view class="statistics">
					<view class="statistics-left" v-if="statisticsIndex" @tap="allCheck">
						<image src="/static/image/shopCar/select.png"  class="checked-image" mode="" >
						</image>
						<text>全选</text>
					</view>
					<view class="statistics-left" v-else @tap="allCheck">
						<image src="/static/image/shopCar/not_select.png"  class="checked-image" mode="" >
						</image>
						<text>全选</text>
					</view>
					<view class="statistics-right" v-if="isCut" @tap="accounts">
						<text>总计：</text><text class="text-color">¥{{total}}</text>
						<view class="btn">
							<text>结算</text>
						</view>
					</view>
					<view class="statistics-right" v-else @tap="delect">
						<view class="btn">
							<text>删除</text>
						</view>
					</view>
				</view>
				<view class="gap"></view>
			</view>
		</view>
		
			<view class="empty-shop-car" v-else>
				<image src="/static/image/shopCar/empty_shop_car.png" class="empty-shop-car-image" mode=""></image>
				<text>当前您的购物车是空的</text>
				<view class="empty-shop-car-btn">
					<text>去逛逛</text>
				</view>
			</view> 
		<slot></slot>
	</view>
</template>

<script>
    import { getCartList,delGoodsCar} from '@/api/home.js'
	export default {
		name: "commonCar",
		data() {
			return {
				isEmpty: true,
				iPhoneX: false,
				datas: {
					goods:[]
				},
				goods:[],
				statisticsIndex:false,
				total:0,
				isCut:true,
				totalNum:0,
			}
		},

		methods: {
			getInit(){
				this.goods = []
                let arr = []
				getCartList().then(res=>{
					if(res.code == 1){ 
				       arr =res.data
                        arr.map(item=>{
                            item.checked = 1
                        })
                        this.goods = arr
					}
				})
			},
			//商品选择
			goodsCheck(  goodsIndex, goodsChecked) {
                console.log(goodsIndex, goodsChecked);
				if (goodsChecked == 1) {
					this.goods[goodsIndex].checked = 2
				} else {
					this.goods[goodsIndex].checked = 1
				}

				
				this.statistics()
			},

			//减少
			sub(goodsIndex, goodsnum){
				console.log( goodsIndex, goodsnum);
				if(goodsnum == 1){
					return
				}else{
					this.goods[goodsIndex].count--
				}
				this.statistics()
			},
			//增加
			add(goodsIndex, goodsnum){
				this.goods[goodsIndex].count++
				this.statistics()
			},
			//全选
			allCheck(){ 
				if(this.statisticsIndex){
					 this.goods.find((itemq,indexq)=>{
					 	itemq.checked = 1
					 })
					this.statisticsIndex = false
				}else{
					this.goods.find((itemq,indexq)=>{
						itemq.checked = 2
					})
					this.statisticsIndex = true
				}
				this.statistics()
			},
			//统计
			statistics(){
				let total = 0 ,totalNum = 0 
				this.goods.find((itemq,indexq)=>{
					if(itemq.checked == 2){
						total = total + itemq.price*itemq.count
						totalNum++
					}
				})
				this.totalNum = totalNum
				this.total = total.toFixed(2)
			},
			cut(){
				this.isCut = !this.isCut
				this.statisticsIndex = true
				this.allCheck()
			},
            //结算
			accounts(){
				let judge = this.judgeSelect()
                let arr = [],that = this
                this.goods.map(item=>{
                    console.log(item)
                    if(item.checked==2){
                        arr.push(item.id)
                    }
                })
				if(judge){
                    let cart_id = ''
                    cart_id = arr.join(',')
					uni.navigateTo({
						url:'/pages/car/confirmOrder?ids='+cart_id
					})
				}else{
					uni.showToast({
						title:'您当前未选择任何商品,结算失败',
						icon:'none'
					})
				}
			},
            // 删除
			delect(){
				let judge = this.judgeSelect()
				 
				let arr = [],that = this
				this.goods.map(item=>{
					if(item.checked==2){
						arr.push(item.id)
					}
				})
				 
				if(judge){
					let cart_id = ''
					cart_id = arr.join(',')
					uni.showModal({
						title:'提示',
						content:'确定删除选中的商品',
						showCancel:true,
						confirmText:'删除',
						success(e) {
							if(e.confirm){
								delGoodsCar({cart_id}).then(res=>{
									if(res.code ==1){
										that.getInit()
										uni.showToast({
											icon:'none',
											title:'删除成功'
										})
									}
								})
							}
						}
					})
					// this.$emit('delect',this.datas)
				}else{
					uni.showToast({
						title:'您当前未选择任何商品,删除失败',
						icon:'none'
					})
				}
				
			},
			judgeSelect(){
				let judge = false
				this.goods.find((itemq,indexq)=>{
					if(itemq.checked == 2){
						judge = true
					}
				})
				return judge
			}
		}
	}
</script>

<style lang="scss" scoped>
	.common-car {
		width:100%;
		min-height: 100vh;
		padding-bottom: 120rpx;
		display: flex;
		flex-direction: column;
		align-items: center; 
	}

	.empty-shop-car {
		width: 750rpx;
		min-height: 680rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.empty-shop-car-image {
			width: 340rpx;
			height: 278rpx;
			margin-top: 102rpx;
		}

		text {
			margin-top: 40rpx;
			font-size: 28rpx;
			font-weight: 400;
			color: #666666;
		}

		.empty-shop-car-btn {
			width: 240rpx;
			height: 90rpx;
			background: #FBBC02;
			border-radius: 200rpx;
			margin-top: 40rpx;
			text-align: center;
			line-height: 90rpx;
			font-size: 30rpx;
			font-weight: 400;
			color: #313133;
		}
	}

	.shop-car {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		.line {
			height: 20rpx;
		}
		.shop-car-header {
			 width: 100%;
			height: 80rpx;
			display: flex;
			flex-direction: row;
			align-items: center; 

			text {
				font-size: 30rpx;
			
				font-weight: 400;
				color: #000;
				&.cut {
						font-size: 28rpx;
						color: #43ABC4;
				}
			}
		}

		.store-box {
			width: 750rpx;
			margin-bottom: 20rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			background-color: #FFFFFF;

			.store-header {
				width: 690rpx;
				height: 78rpx;
				padding: 0 30rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				font-size: 28rpx;
				font-weight: 400;
				color: #313133;

				.checked-image {
					width: 36rpx;
					height: 36rpx;
				}

				text {
					font-size: 28rpx;
					font-weight: 400;
					color: #313133;
					margin-left: 20rpx;
				}

				.label {
					width: 14rpx;
					height: 24rpx;
					margin-left: 10rpx;
					margin-top: 5rpx;
				}
			}

			.goodsInfo {
				width: 690rpx; 
				padding: 30rpx 0;
				display: flex;
				flex-direction: row;
				align-items: center;
				border-bottom: 2rpx solid #EDEDED;

				&:nth-last-child(1) {
					border: none;
				}

				.checked-image {
					width: 36rpx;
					height: 36rpx;
				}

				.goodsInfo-right {
					width: 634rpx; 
					margin-left: 30rpx;
					display: flex;
					flex-direction: row;
					align-items: center;

					.goods-image {
						width: 120rpx;
						height: 120rpx;
					}

					.goodsInfo-box {
						flex: 1;
						margin-left: 24rpx;
						display: flex;
						flex-direction: column;
						align-items: center;

						.goods-name {
							width: 428rpx;
							font-size: 30rpx;
							font-weight: 400;
							color: #000;
						}

						.spe {
							width: 428rpx;
							margin-top: 10rpx;
							font-size: 24rpx;
							font-weight: 400;
							color: #919298;
						}

						.goods-box {
							width: 428rpx;
							margin-top: 18rpx;
							display: flex;
							flex-direction: row;
							align-items: center;
							justify-content: space-between;

							.goods-price {
								font-size: 24rpx;
								font-weight: 400;
								color: #000;
								text {
									font-size: 30rpx;font-weight: bold;
								}
							}

							.goods-num-box { 
								height: 46rpx;
								display: flex;
								flex-direction: row;
								align-items: center;

								.goods-image {
									width: 34rpx;
									height: 34rpx;
									text-align: center;
									line-height: 28rpx;
									border: 1rpx solid #DBDBDB;
									border-radius: 17rpx;
									font-size:24rpx;
								}

								.goods-num {
									width: 45rpx;
									height: 34rpx;
									text-align: center;
									line-height:34rpx;
									font-size: 28rpx;
									font-weight: 400;
									color: #333; 
								}
							}
						}
					}
				}
			}
		}

		.statistics-box {
			width: 750rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			background-color: #FFFFFF;
			position: fixed;
			bottom: 0;

			.statistics {
				width:100%; 
				height: 100rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: space-between;

				.statistics-left {
					width: 120rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: space-between;

					image {
						width: 36rpx;
						height: 36rpx;
					}

					text {
						font-size: 30rpx;
						font-weight: 400;
						color: #313133;
					}
				}

				.statistics-right {
					width: 600rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: flex-end;

					.btn {
						width: 190rpx;
						height: 90rpx;
						margin-left: 20rpx;
						background: #43ABC4;
						border-radius: 45rpx;
						text-align: center;
						line-height: 90rpx;
						font-size: 28rpx;
						font-weight: 400;
						
						text {
							color: #fff; 
						}
					}

					text {
						font-size: 30rpx;
						font-weight: 400;
						color: #333;
					}

					.text-color {
						color:#000;
						font-size: 26rpx;
						font-weight: bold;
					}
				}
			}

			.gap {
				width: 750rpx;
				// height: 40rpx;
			}
		}
	}
</style>
